﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using QConnectSDK;
using QConnectSDK.Context;

namespace Portal.Web.Controllers
{
    public class LoginController : BaseFrontController
    {
        //
        // GET: /Login/
        private const string RETURNURL = "RETURNURL";

        public ActionResult Index()
        {
            return View(CreateFrontViewData<FrontBaseViewData>());
        }



        public ActionResult ToQq(string returnUrl)
        {
            this.Session[RETURNURL] = returnUrl ?? Url.Action("Index", "Home");
            var context = new QzoneContext();
            string state = Guid.NewGuid().ToString().Replace("-", "");
            Session["requeststate"] = state;
            const string scope = "get_user_info,add_share,list_album,upload_pic,check_page_fans,add_t,add_pic_t,del_t,get_repost_list,get_info,get_other_info,get_fanslist,get_idolist,add_idol,del_idol,add_one_blog,add_topic,get_tenpay_addr";
            var authenticationUrl = context.GetAuthorizationUrl(state, scope);
            return new RedirectResult(authenticationUrl);
        }
        public ActionResult QQConnect()
        {
            if (Request.Params["code"] != null)
            {
                var verifier = Request.Params["code"];
                var state = Request.Params["state"];
                string requestState = Session["requeststate"].ToString();
                if (state == requestState)
                {
                    var qzone = new QOpenClient(verifier, state);
                    var currentUser = qzone.GetCurrentUser();
                    if (this.Session["QzoneOauth"] == null)
                    {
                        this.Session["QzoneOauth"] = qzone;
                    }
                    var friendlyName = currentUser.Nickname;
                    var isPersistentCookie = true;
                    FormsAuthentication.SetAuthCookie(qzone.OAuthToken.OpenId, isPersistentCookie, friendlyName);
                    return Redirect(Url.Action("Index", "Home"));
                }
            }
            return View();
        }

        public ActionResult Throw()
        {
            throw new ArgumentException("\"{0}\" 是空的GUID.".FormatWith(1), "2234asfd");
       
            return View();
        }

    }
}
